import Vue from 'vue'
import VueRouter from 'vue-router'
import request from '../network/request'
import store from '../store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
  },
  // 登录
  {
    path: '/logIn',
    name: 'logIn',
    component: () => import('../views/personalCenter/notLogIn/logIn.vue'),
  },
  // 注册
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/personalCenter/notLogIn/register.vue'),
  },
  // 忘记密码
  {
    path: '/forgetPassword',
    name: 'forgetPassword',
    component: () => import('../views/personalCenter/notLogIn/forgetPassword.vue'),
  },
  // 个人中心菜单栏
  {
    path: '/menu',
    name: 'menu',
    component: () => import('../views/personalCenter/alreadyLogIn/menu.vue'),
    children: [
      { // 签到日历
        path: '/menu/signInCalendar',
        name: 'signInCalendar',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/signInCalendar.vue'),
      }, // 会员中心
      { // 修改基本信息 
        path: '/menu/modifyInformation',
        name: 'modifyInformation',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/modifyInformation.vue'),
      },{ // 修改密码
        path: '/menu/modifyPassword',
        name: 'modifyPassword',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/modifyPassword.vue'),
      },{ // 修改手机号
        path: '/menu/modifyPhone',
        name: 'modifyPhone',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/modifyPhone.vue'),
      },{ // 修改邮箱
        path: '/menu/modifyMailbox',
        name: 'modifyMailbox',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/modifyMailbox.vue'),
      }, // 服务中心
      { // 学习记录
        path: '/menu/recordStudy',
        name: 'recordStudy',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/recordStudy.vue'),
      },{ // 场馆预约
        path: '/menu/recordVenue',
        name: 'recordVenue',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/recordVenue.vue'),
      },{ // 活动记录
        path: '/menu/recordActivity',
        name: 'recordActivity',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/recordActivity.vue'),
      },{ // 我的消息
        path: '/menu/myMessage',
        name: 'myMessage',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/myMessage.vue'),
      }, // 我的积分
      { // 积分记录
        path: '/menu/integralRecord',
        name: 'integralRecord',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/integralRecord.vue'),
      },{ // 积分规则
        path: '/menu/integralRule',
        name: 'integralRule',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/integralRule.vue'),
      }, // 我的收藏
      { // 收藏学习
        path: '/menu/collectStudy',
        name: 'collectStudy',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/collectStudy.vue'),
      },{ // 收藏场馆
        path: '/menu/collectVenue',
        name: 'collectVenue',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/collectVenue.vue'),
      },{ // 收藏活动
        path: '/menu/collectActivity',
        name: 'collectActivity',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/collectActivity.vue'),
      }, // 璀璨空间
      { // 发布记录
        path: '/menu/publishRecord',
        name: 'publishRecord',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/publishRecord.vue'),
      },{ // 发布作品
        path: '/menu/publishWorks',
        name: 'publishWorks',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/publishWorks.vue'),
      }, // 我的社团
      { // 加入的社团
        path: '/menu/clubJoin',
        name: 'clubJoin',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/clubJoin.vue'),
      },{ // 社团公告
        path: '/menu/clubNotice',
        name: 'clubNotice',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/clubNotice.vue'),
      },{ // 社团分采
        path: '/menu/clubSeparate',
        name: 'clubSeparate',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/clubSeparate.vue'),
      },{ // 成员管理
        path: '/menu/clubMember',
        name: 'clubMember',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/clubMember.vue'),
      },{ // 帮助中心
        path: '/menu/helpCenter',
        name: 'helpCenter',
        component: () => import('../views/personalCenter/alreadyLogIn/menu/helpCenter.vue'),
      },
    ]
  },
  // 课程详情
  {
    path: '/muke/courseDetails',
    name: 'courseDetails',
    component: () => import('../views/admiringStudy/courseDetails.vue'),
  },
  // 调查问卷详情
  {
    path: '/dcwj/questionnaireDetails',
    name: 'questionnaireDetails',
    component: () => import('../views/questionnaire/questionnaireDetails.vue'),
  },
]



const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{ 
  let arr =[]
   request.getRouteList({}).then(res=>{
      store.commit('setRoute',res.data)
      for(let i in res.data){
        let item = res.data[i].url.split('.')[0]
        if(to.path == item){
            if(res.data[i].is_login == 1){
                 if(localStorage.getItem("token") == undefined || null || ''){
                      this.$message.error('请先登录');
                      router.push('/login')
                      return;
                 }
            }
        }
         arr.push({path:item,component:() => import('../views'+item+'.vue')})
      }
      // console.log(arr)
      router.addRoutes(arr)
    })
   next()
})
export default router
